﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MxitAPI {

	public class ReceivedMessage : MxitMessage {

		private static log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
		
		
		public MxitDateTime DateTime { get; set; }  /// The date this message was sent
		public MessageType MessageType { get; set; }  /// The type of message
		public int? MxitMessageId { get; set; }  /// The id of the message sent by the mxit servers (may be blank)
		// Todo remove this once flags are being handled properly
		public string Flags { get; set; }  /// Flags for the message (encrypted, markup, etc.)
		public bool Read { get; set; }  /// Stores whether this message has been "read"
		// Todo not sure if I need a unique message id?
		public int MessageId { get; private set; }  /// A message Id that is unique in this application
		private static int currentMessageId = 0;
		
		private ReceivedMessage() {
			MessageId = currentMessageId;
			++currentMessageId;
		}
		
		
		/**
		 * Create a new message from a rawMessage ( made with data returned by the mxit server ).
		 * Creates a skeleton contact if a match for the ContactAddress doesnt exist
		 * @param rawMessage Message data received from the mxit servers
		 */
        public ReceivedMessage( RawMessage rawMessage ) : this() {
		
			// If this contact does not exist, create a skeleton of a contact using the ContactAddress
			if ( Contacts.Instance.GetContact( rawMessage.ContactAddress ) == null ) {
				log.Debug( "No contact to assign this message to" );
				Contacts.Instance.AddContact( rawMessage.ContactAddress );
			}
			
			// Populate the message fields
			DateTime = new MxitDateTime( rawMessage.Date );
			if ( rawMessage.MessageType == 0 )
				MessageType = MessageType.Normal;
			else
				MessageType = rawMessage.MessageType;
			MxitMessageId = rawMessage.Id;
			Flags = rawMessage.Flags;
			MessageText = rawMessage.MessageText;
			ContactId = Contacts.Instance.GetContact( rawMessage.ContactAddress ).ContactId;
			
				
		}
	}


}
